package org.itboys.core.service.mongo.front;

import org.apache.commons.lang.StringUtils;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayAddress;
import org.itboys.core.util.PhoneUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/1
 * Description: 收货地址服务类
 */

@Service
public class SundayFrontAddressService extends BaseMongoService<SundayAddress> {
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayAddress> getEntityClass() {
        return SundayAddress.class;
    }

    /**
     * 获取当前用户的所有收货地址
     * @param memberId
     * @return
     */
    public List<SundayAddress> select(HttpServletRequest request, Long memberId){
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("memberId", memberId);
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("sort","isDefault");
        param.put("order","DESC");
        List<SundayAddress> list = super.pageQuery(request,param).getData();
        return list;
    }

    /**
     * 获取收货地址详情
     * @param id
     * @return
     */
    public SundayAddress getDetailAddress(Long id){
        SundayAddress address = new SundayAddress();
        if(id == null || id == 0L){
            address.setId(0);
            address.setIsDefault(1);
        }else{
            address = super.getById(id);
        }
        return address;
    }

    /**
     * 获取默认收获地址
     * @param memberId
     * @return
     */
    public SundayAddress getDefaultAddress(Long memberId){
        Map<String,Object> param =new HashMap<>();
        param.put("memberId", memberId);
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("isDefault", SundayCommonConstants.YES);
        List<SundayAddress> sundayAddressList=super.list(param);
        if (sundayAddressList==null){
            throw new ErrorException("请添加收货地址");
        }
        return sundayAddressList.get(0);
    }


    /**
     * 新增或修改收货地址
     * @param lable
     * @param memberId
     * @param receiveName
     * @param receiveMobile
     * @param provinceId
     * @param provinceName
     * @param cityId
     * @param cityName
     * @param districtId
     * @param districtName
     * @param streetId
     * @param streetName
     * @param roadName
     * @param villageId
     * @param villageName
     * @param detailedAddress
     * @param isDefault
     */
    public void save(
            Long    id,
            String  lable,
            Long    memberId,
            String  receiveName,
            String  receiveMobile,
            Long    provinceId,
            String  provinceName,
            Long    cityId,
            String  cityName,
            Long    districtId,
            String  districtName,
            Long    streetId,
            String  streetName,
            String  roadName,
            Long    villageId,
            String  villageName,
            String  detailedAddress,
            Integer isDefault
    ){
        if (StringUtils.isEmpty(receiveMobile)) {
            throw new ErrorException("手机号为空");
        }
        if (!PhoneUtil.isMobileNumber(receiveMobile)){
            throw new ErrorException("手机号格式有误!");
        }
        //如果设置该地址为默认地址，先将原有的默认地址设置为NO
        if(isDefault != null && SundayCommonConstants.YES.equals(isDefault)){
            setDefaultNO(memberId);
        }
        //新增或修改地址
        SundayAddress address=new SundayAddress(
                lable,
                memberId,
                receiveName,
                receiveMobile,
                provinceId,
                provinceName,
                cityId,
                cityName,
                districtId,
                districtName,
                streetId,
                streetName,
                roadName,
                villageId,
                villageName,
                detailedAddress,
                isDefault);
        //查询出原有的默认地址
        if(LongUtil.isNotZreo(id)){
            //修改原有的默认地址
            super.updateExceptEmpty(id,address);
        }else {
            super.save(address);
        }
    }

    /**
     * 设置默认地址
     * @param memberId
     */
    public void setDefaultNO(Long memberId){
        Map<String, Object> param=new HashMap<>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("isDefault",SundayCommonConstants.YES);
        param.put("memberId",memberId);
        List<SundayAddress> address = super.list(param);
        if(ListUtil.isNotNull(address)){
            super.update(address.get(0).getId(),"isDefault",SundayCommonConstants.NO);
        }
    }

    /**
     * 根绝id获取一个收货地址
     * @param id
     * @return
     */
    public SundayAddress findOne(Long id){
        SundayAddress address = super.getById(id);
        return address;
    }

}
